package com.haotunet.app.goldtreasure.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.adapter.MainPagerAdapter;
import com.haotunet.app.goldtreasure.fragment.FoundFragment;
import com.haotunet.app.goldtreasure.fragment.GoldFinancialFragment;
import com.haotunet.app.goldtreasure.fragment.HomePageFragment;
import com.haotunet.app.goldtreasure.fragment.MerchantServiceFragment;
import com.haotunet.app.goldtreasure.fragment.PersonalCenterFragment;
import com.haotunet.app.goldtreasure.utils.ApkUpdateControl;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;

@Route(path = "/mine/main")
public class MainActivity extends BaseActivity implements View.OnClickListener {
    // 点击两次退出
    long firstTime = 0;
    private ViewPager mViewpager;
    private View[] views = new View[5];
    private Fragment[] fragment = new Fragment[5];
    private MainPagerAdapter adapter;
    private PushAgent mPushAgent;
    private PersonalCenterFragment personalCenterFragment;
    private HomePageFragment homePageFragment;
    private FoundFragment foundFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPushAgent = PushAgent.getInstance(this);
        mPushAgent.onAppStart();
        mPushAgent.enable(new IUmengRegisterCallback() {

            @Override
            public void onRegistered(final String arg0) {

            }
        });
        mPushAgent.setDebugMode(true);
        initView();
        ApkUpdateControl.getInstance().checkVersion(MainActivity.this, 1);
    }

    private void initView() {
        mViewpager = (ViewPager) findViewById(R.id.main_viewpager);
        View mTabFinance = findViewById(R.id.tab_financal_text);
        mTabFinance.setOnClickListener(this);
        views[0] = mTabFinance;
        View mTabSeller = findViewById(R.id.tab_near_text);
        mTabSeller.setOnClickListener(this);
        views[1] = mTabSeller;
        View mTabHome = findViewById(R.id.tab_home);
        mTabHome.setOnClickListener(this);
        views[2] = mTabHome;
        View mTabFound = findViewById(R.id.tab_found_txt);
        mTabFound.setOnClickListener(this);
        views[3] = mTabFound;
        View mTabPersonal = findViewById(R.id.tab_personal_txt);
        mTabPersonal.setOnClickListener(this);
        views[4] = mTabPersonal;
        String type = "";
        if (getIntent().hasExtra("type")) {
            type = getIntent().getStringExtra("type");
        }
        fragment[0] = GoldFinancialFragment.newInstance();
        fragment[1] = MerchantServiceFragment.newInstance(type);
        homePageFragment = HomePageFragment.newInstance();
        fragment[2] = homePageFragment;
        foundFragment = FoundFragment.newInstance();
        fragment[3] = foundFragment;
        personalCenterFragment = PersonalCenterFragment.newInstance();
        fragment[4] = personalCenterFragment;
        adapter = new MainPagerAdapter(getSupportFragmentManager());
        adapter.add(fragment[0]);
        adapter.add(fragment[1]);
        adapter.add(fragment[2]);
        adapter.add(fragment[3]);
        adapter.add(fragment[4]);
        mViewpager.setAdapter(adapter);
        mViewpager.setOffscreenPageLimit(adapter.getCount());

        if ("exchangeSuccess".equals(type)) {
            setTabView(1);
        } else {
            setTabView(2);
        }
    }

    public void setTabView(int cur) {
        mViewpager.setCurrentItem(cur, false);
        if (cur == 4) {
            personalCenterFragment.showUnLoginDialog();
        }
        for (int i = 0; i < views.length; i++) {

            if (cur == i) {
                views[i].setSelected(true);
            } else {
                views[i].setSelected(false);
            }

        }
    }

    @Override
    public void onClick(View v) {
        int position = 0;
        switch (v.getId()) {
            case R.id.tab_financal_text:
                position = 0;
                break;
            case R.id.tab_near_text:
                position = 1;
                break;
            case R.id.tab_home:
                position = 2;
                break;
            case R.id.tab_found_txt:
                position = 3;
                break;
            case R.id.tab_personal_txt:
                position = 4;
                break;
        }
        setTabView(position);
    }

    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            long secondTime = System.currentTimeMillis();
            if (secondTime - firstTime > 1000) { // 如果两次按键时间间隔大于1000毫秒，则不退出
                UIHelper.showToast(MainActivity.this, getString(R.string.again_exit_the_program));
                firstTime = secondTime; // 更新firstTime
                return true;
            } else {
                System.exit(0); // 退出程序
            }
        }
        return super.onKeyUp(keyCode, event);
    }

    public void skipToDisCoverey() {
        setTabView(3);
        foundFragment.switchTab(1);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        String type = intent.getStringExtra("type");
        if ("invest".equals(type)) {
            setTabView(0);
        }
        if ("forceQuit".equals(type) || "resetPwd".equals(type) || "register".equals(type)) {
            personalCenterFragment.showUnLoginDialog();
            setTabView(4);
        }
        if ("orderFail".equals(type)) {
            setTabView(2);
        }
        if ("wechatLogin".equals(type)) {
            setTabView(4);
        }
    }
}
